import re

from netmiko.huawei.huawei import HuaweiSSH

username = "yunwei-xunjian"
password = "Rn%kmh4s,4"
ip = "10.2.161.3"

cmd_line = ['display version', 'display cpu-usage', 'display memory-usage', 'display device']

switch = {
    'device_type': 'huawei',
    'ip': ip,
    'username': username,
    'password': password,
}

net_connect = HuaweiSSH(**switch)
print("恭喜，ssh成功登录")

#print("设备名：" + str(net_connect.find_prompt().strip('<>')))
print("[XJ]系统-设备名#{}#正常#获取不到设备信息#无".format(str(net_connect.find_prompt().strip('<>'))))
net_connect.send_command("screen-length 0 temporary")
for cmd in cmd_line:
    cmd_result = net_connect.send_command(cmd)
    regex_str = []
    if 'VRP (R) software' in cmd_result:
        regex_str = '\(\w*\d\d.*\)'
        version = (re.search(regex_str, cmd_result))
        label = version.group().strip('()')
        #print("版本: ",label)
        print("[XJ]系统-版本#{}#正常#获取不到版本异常#无".format(label))
        cmd_result = ''
    if 'CPU ' in cmd_result:
        regex_str = '\d*.\d*.\%'
        cpu_usage = (re.search(regex_str, cmd_result))
        label=cpu_usage.group().strip(' ')
        #print("CPU使用率: ",label)
        if int(label) < 50:
            print("[XJ]性能-CPU使用率#{}#正常#小于50%#无".format(label))
        else:
            print("[XJ]性能-CPU使用率#{}#异常#大于等于50%#请检查CPU情况".format(label))
        cmd_result = ''
    if 'Memory ' in cmd_result:
        regex_str = '\d*.\%'
        memory = (re.search(regex_str, cmd_result))
        label = memory.group()
        #print("内存使用率: ",label)
        if int(label) < 50:
            print("[XJ]性能-内存使用率#{}#正常#小于50%#无".format(label))
        else:
            print("[XJ]性能-内存使用率#{}#异常#大于等于50%#请检查内存情况".format(label))
        cmd_result = ''
    if 'Device ' in cmd_result:
        if 'Abnormal' in cmd_result:
            print("状态: Abnormal")
        elif 'WrongType' in cmd_result:
            print("状态: WrongType")
        elif 'Unregistered' in cmd_result:
            print("状态: Unregistered")
        elif 'Off' in cmd_result:
            print("状态: Off")
        elif 'Offline' in cmd_result:
            print("状态: Offline")
        else:
            print("[XJ]状态-运行状态#Normal#正常#状态信息#无")
        cmd_result = ''
net_connect.disconnect()
